package edu.iyte.ceng431.hw2.pos.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import edu.iyte.ceng431.hw2.pos.model.Cash;
import edu.iyte.ceng431.hw2.pos.model.CreditCard;
import edu.iyte.ceng431.hw2.pos.model.Payment;
import edu.iyte.ceng431.hw2.pos.model.Register;
import edu.iyte.ceng431.hw2.pos.view.PaymentDialog;

public class PaymentController {

    private Register currentRegister;
    private PaymentDialog paymentDialog;
    private double total;

    public PaymentController(PaymentDialog newPaymentDialog,
            Register currentRegister) {
        this.paymentDialog = newPaymentDialog;
        this.total = Double.parseDouble(newPaymentDialog.getCashTextField().getText());
        this.currentRegister = currentRegister;
        this.paymentDialog.addPayListener(new PayListener());
        this.paymentDialog.addCancelListener(new CancelListener());
        this.paymentDialog.addCashTextListener(new CashTextListener());
        this.paymentDialog.addCreditTextListener(new CreditTextListener());
    }

    /**
     * @return the total
     */
    public double getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(double total) {
        this.total = total;
    }

    class PayListener implements ActionListener {

    	public void actionPerformed(ActionEvent e) {
			double cash, card;

			try {
				card = Double.parseDouble(paymentDialog
						.getCreditCardTextField().getText());
			} catch (NumberFormatException e1) {
				card = 0;
			}

			if (card > 0
					&& paymentDialog.getCreditCardNumberTextField().getText().equals("")) {
				paymentDialog.getCreditCardNumberLabel().setText(
						"Error: Type your card id here");
			} else {
				if (card > 0) {
					Payment cashPayment = new CreditCard(card, paymentDialog
							.getCreditCardNumberTextField().getText());
					currentRegister.getCurrentsale().addPayment(cashPayment);
				}

				try {
					cash = Double.parseDouble(paymentDialog.getCashTextField()
							.getText());
				} catch (NumberFormatException e1) {
					cash = 0;
				}
				if (cash > 0) {
					Payment cashPayment = new Cash(cash);
					currentRegister.getCurrentsale().addPayment(cashPayment);
				}

				if (cash + card < getTotal() || card > getTotal()) {
					JOptionPane.showMessageDialog(paymentDialog,
							"Invalid Payment", "Error",
							JOptionPane.ERROR_MESSAGE);
				} else {
					double excess = cash + card - getTotal();
					if (excess > 0)
					{
						currentRegister.addBalance(-1 * excess);
						JOptionPane.showMessageDialog(paymentDialog,
								"Change Amount is " + excess, "Change Amount",
								JOptionPane.INFORMATION_MESSAGE);
					}
					currentRegister.makeSale();
					paymentDialog.dispose();
				}
			}

		}
    }

    class CancelListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            paymentDialog.dispose();
        }
    }

    class CashTextListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                double temp = Double.parseDouble(paymentDialog.getCashTextField().getText());
                if (temp < getTotal()) {
                    paymentDialog.getCreditCardTextField().setText(
                            String.valueOf((getTotal() - temp)));
                }
            } catch (NumberFormatException nfex) {
                //do nothing
            }
        }
    }

    class CreditTextListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                double temp = Double.parseDouble(paymentDialog.getCreditCardTextField().getText());
                if (temp < getTotal()) {
                    paymentDialog.getCashTextField().setText(
                            String.valueOf((getTotal() - temp)));
                    paymentDialog.getCreditCardNumberTextField().setVisible(
                            true);
                    paymentDialog.getCreditCardNumberLabel().setVisible(true);
                }
            } catch (NumberFormatException nfex) {
                //do nothing
            }
        }
    }
}
